﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YmtUserAuth.DataImport
{
    public class DataImportAppService
    {
        public static void Run()
        {
            var filePath = @"E:\works\ymatou\project2\Ymatou\Main\YmatouAuth\data\ymt_user2.csv";
            var watch = Stopwatch.StartNew();
            var result = ReadFile.SyncRead(filePath);
            var user = FillUserContainer.Fill(result);
            MongoRepository<UserData2>.Insert(user);
            Console.WriteLine("总耗时 {0}", watch.ElapsedMilliseconds / 1000.0);
        }

        public static void Run2()
        {
            var tmpPath = System.Configuration.ConfigurationManager.AppSettings["filepath"];
            var filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, tmpPath);//@"E:\works\ymatou\project2\Ymatou\Main\YmatouAuth\data\ymt_user2.csv";
            Console.WriteLine("导入数据文件路径：{0}", filePath);
            var watch = Stopwatch.StartNew();
            var result = ReadFile.SyncRead(filePath);
            var user = FillUserContainer.FillUserData(result);

            try
            {

                MongoRepository<UserCredential>.Insert("ULC_L_201402", "Ymt_Login_Auth_201402", user.Item1["ULC_L_201402"].Distinct(new UserCredentialEqualityComparer()));              
            }
            catch (Exception ex)
            {
                Console.WriteLine("用户sLogin凭证数据导入异常 {0}", ex.ToString());
            }
            try
            {
                MongoRepository<UserCredential>.Insert("ULC_E_201402", "Ymt_Login_Auth_201402", user.Item1["ULC_E_201402"].Distinct(new UserCredentialEqualityComparer()));
            }
            catch (Exception ex)
            {
                Console.WriteLine("用户Email凭证数据导入异常 {0}", ex.ToString());
            }
            try
            {
                MongoRepository<UserData>.Insert(user.Item2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("用户数据导入异常 {0}", ex.ToString());
            }
            Console.WriteLine("总耗时 {0}", watch.ElapsedMilliseconds / 1000.0);
        }
    }
}
